from django.db import models
from RBAC.models import Staff


# Create your models here.


# 流程表
class Flow(models.Model):
    fname = models.CharField(max_length=32, verbose_name='流程名称')
    status = models.BooleanField(default=False, verbose_name='流程状态')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'flow'
        verbose_name_plural = '流程表'

    def __str__(self):
        return self.fname


# 节点表
class Node(models.Model):
    nname = models.CharField(max_length=32, verbose_name='节点名称')
    flow = models.ForeignKey(Flow, on_delete=models.CASCADE, verbose_name='流程')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'node'
        verbose_name_plural = '流程节点表'

    def __str__(self):
        return self.nname


class AttrType(models.Model):
    title = models.CharField(max_length=32, verbose_name='属性类型')

    class Meta:
        db_table = 'attr_type'
        verbose_name_plural = '节点属性类型表'

    def __str__(self):
        return self.title


# 节点属性表
class NodeAttribute(models.Model):
    aname = models.CharField(max_length=32, verbose_name='节点属性名称')
    type = models.ForeignKey(AttrType, on_delete=models.CASCADE, verbose_name='属性实例')
    node = models.ForeignKey(Node, on_delete=models.CASCADE, verbose_name='节点')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'node_attr'
        verbose_name_plural = '节点属性表'

    def __str__(self):
        return self.aname


# 流程实例表
class FlowInstance(models.Model):
    staff = models.ForeignKey(Staff, on_delete=models.CASCADE, verbose_name='发起人')
    flow = models.ForeignKey(Flow, on_delete=models.CASCADE, verbose_name='流程')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'flow_instance'
        verbose_name_plural = '流程审批表'

    def __str__(self):
        return self.flow.fname


# 属性实例表
class AttrInstance(models.Model):
    iname = models.CharField(max_length=32, verbose_name='属性实例名称')
    flow_instance = models.ForeignKey(FlowInstance, on_delete=models.CASCADE, verbose_name='节点')
    attr = models.ForeignKey(NodeAttribute, on_delete=models.CASCADE, verbose_name='属性')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'attr_instance'
        verbose_name_plural = '属性实例表'

    def __str__(self):
        return self.iname


# 流程审批表
class FlowApproval(models.Model):
    NODE_CHOICES = ((1, '待审核'), (2, '已通过'), (3, '已驳回'))
    FLOW_CHOICES = ((1, '待审核'), (2, '已审核'), (3, '已驳回'))
    comment = models.TextField(default='', verbose_name='批注')
    staff = models.ForeignKey(Staff, on_delete=models.CASCADE, verbose_name='审批人')
    instance = models.ForeignKey(FlowInstance, on_delete=models.CASCADE, verbose_name='流程实例')
    node_status = models.SmallIntegerField(default=1, choices=NODE_CHOICES, verbose_name='节点状态')
    flow_status = models.SmallIntegerField(default=1, choices=FLOW_CHOICES, verbose_name='流程状态')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'flow_approval'
        verbose_name_plural = '审批表'

    def __str__(self):
        return self.comment
